﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Dogshow.Win.DataAccess;
using Dogshow.Win.Models;
using Ninject;
using Shell;
using System.Linq;
using Shell.Events;
using Dogshow.Win.Helpers;

namespace Dogshow.Win.Views
{
    public partial class KinoloskaDrustvaView : ViewBase
    {
        private BindingList<KinoloskoDrustvo> m_KinoloskaDrustva = new BindingList<KinoloskoDrustvo>();

        private readonly IDataService m_DataService = ((IKernel) Bootstrap.Bootstrapper.Container).Get<IDataService>();

        public KinoloskaDrustvaView()
        {
            InitializeComponent();
            kinoloskoDrustvoBindingSource.DataSource = m_KinoloskaDrustva;
            kinoloskoDrustvoBindingSource.CurrentChanged += (s, e) =>
                                                            {
                                                                layoutControl2.Enabled = Current != null;
                                                            };
            layoutControl2.Enabled = false;
        }

        protected override void OnLoad(EventArgs e)
        {
            OnRefresh();
        }

        public override void OnRefresh()
        {
            var kinoloskaDrustva = m_DataService.VratiKinoloskaDrustva().Select(ModelMapper.Map).ToList();
            m_KinoloskaDrustva = new BindingList<KinoloskoDrustvo>(kinoloskaDrustva);
            kinoloskoDrustvoBindingSource.DataSource = m_KinoloskaDrustva;
            mestoBindingSource.DataSource = m_DataService.VratiMesta1();
            izborMesta1.RefreshData();
            izborMesta1.SetupBinding(kinoloskoDrustvoBindingSource, "Mesto");
        }

        public override void OnNew()
        {
            var newItem = m_KinoloskaDrustva.AddNew();
            kinoloskoDrustvoBindingSource.MoveTo(newItem);
            textEdit1.Focus();
        }

        public override void OnSave()
        {
            if (Current != null)
            {
                if (!Current.IsValid)
                {
                    OnAlertChanged(new WarningAlertEventArgs("Podaci nisu validni: " + Current.GetErrors()));
                    return;
                }
                m_DataService.Sacuvaj(ModelMapper.Map(Current));
                OnRefresh();
            }
        }

        private KinoloskoDrustvo Current
        {
            get { return kinoloskoDrustvoBindingSource.Current as KinoloskoDrustvo; }
        }
    }
}